import os.path
import re
import argparse


class findP:
    def __init__(self, filepath, resSavePath, timeMax, keyword):
        self.filepath = filepath
        self.resSavePath = resSavePath
        self.text = None
        self.resText = None
        self.logFileName = None
        self.keyword = keyword
        self.timeMax = timeMax

    def saveText(self):
        self.resText = os.path.join(self.resSavePath, str(self.logFileName) + '.txt')
        self.resText = open(self.resText, "w")

    def readFile(self):
        with open(self.filepath, "r", encoding="utf-8") as file:
            self.text = file.readlines()
            self.logFileName = os.path.basename(self.filepath)
            self.saveText()

    def findKeywords(self):
        self.readFile()
        for line_numer, line in enumerate(self.text, start=1):
            if self.keyword in line:
                lineWord = line.strip()
                timecost = float(lineWord.split("(s):")[1])
                if timecost > self.timeMax:
                    self.resText.write(str(line.strip()) + '\n')
                    # print(f"{line.strip()}" + '\n')


def main(args_):
    currBasePath = os.getcwd()
    paramsFile = os.path.join(currBasePath, str("param.txt"))
    with open(paramsFile, "r", encoding="utf-8") as file:
        param = file.readlines()
        timeMax = float(param[2].strip())
        filepath = param[0].strip()
        resPath = param[1].strip()
        keyword = param[3].strip()
        findtime = findP(str(filepath), str(resPath), timeMax, keyword)
        findtime.findKeywords()


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    # parser.add_argument("paramName", help="参数文件名称")
    # parser.add_argument("input_path", help="输入文件地址")
    # parser.add_argument("output_path", help="输出文件地址")
    # parser.add_argument("timeMax", help="限制最大时间")
    # parser.add_argument("keyword", help="关键词")
    args = parser.parse_args()
    main(args)
